package com.uah;

import java.util.List;
import java.util.Map;

import com.scraper.Contact;
import com.scraper.Link;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

public class DynamicLayoutCreation {
	
	// Adds a single button to a row
	public static void createNewRow(Link link, String goToClassTag, TableLayout layout, Context className){
		TableRow tr = new TableRow(className);
	  	  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f);
	  	  tr.setLayoutParams(lp);

	  	  Button newButton = new Button(className);
	  	  newButton.setText(link.description);
	  	  newButton.setTag(goToClassTag + link.url);
	  	  newButton.setPadding(0, 0, 0, 0);

	  	  tr.addView(newButton);
	  	  layout.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
	  	  
	  	  newButton.setOnClickListener(btnLinkListener);
	}
	
	// Adds multiple buttons to a row
	public static void createNewRow(Link[] links, String goToClassTag, TableLayout layout, Context className){
		TableRow tr = new TableRow(className);
  	  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f);
  	  tr.setLayoutParams(lp);

  	  for(int i=0;i<links.length;i++)
  	  {
  		  Button newButton = new Button(className);
  		  newButton.setText(links[i].description);
  		  newButton.setTag(goToClassTag + links[i].url);
  		  newButton.setPadding(0, 0, 0, 0);
  		  tr.addView(newButton);
  		  newButton.setOnClickListener(btnLinkListener);
  	  }
  	  layout.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
	}
	
	// Adds a row with one textview
	public static void createNewRow(String label, TableLayout layout, Context className){
		TableRow tr = new TableRow(className);
		  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f);
		  
		  tr.setLayoutParams(lp);

		  TextView newTextView = new TextView(className);
		  newTextView.setText(label);
		  
		  tr.addView(newTextView);
		  layout.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
	}
	
	// Adds a row with one textview, could not get text wrap to work correctly
	public static void createNewRowWrapping(String label, TableLayout layout, Context className){
		TableRow tr = new TableRow(className);
		  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f);
		  
		  tr.setLayoutParams(lp);

		  TextView newTextView = new TextView(className);
		  newTextView.setText(label);
		  if(label.length() > 100){
			 newTextView.setWidth(0);	// Allows for text wrapping 
		  }
		  
		  tr.addView(newTextView);
		  layout.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
	}
	
	// Adds multiple textviews to one row
	// For example, class schedule viewing
	public static void createNewRow(String[] labels, TableLayout layout, Context className){
		TableRow tr = new TableRow(className);
	  	  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f);
	  	  tr.setLayoutParams(lp);

	  	  for(int i=0;i<labels.length;i++)
	  	  {
	  		TextView newTextView = new TextView(className);
	  		newTextView.setText(labels[i]);
	  		newTextView.setTextSize(12);	//Uses smaller text than other pages
	  	  	tr.addView(newTextView);
	  	  }
	  	  layout.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
	}
	
	//Used to populate from College Department data
	public static void createNewRow(Map<String, List<Link>> map, TableLayout layout, Context className){
	  	for (Map.Entry<String,List<Link>> entry : map.entrySet())
    	{
    	    String key = entry.getKey();
    	    List<Link> links = entry.getValue();
    	    DynamicLayoutCreation.createNewRow(key, layout, className);
    	    for(Link link : links)
    	    {
    	    	 DynamicLayoutCreation.createNewRow(link, "", layout, className);
    	    }
    	}
	}
	
	//Used to populate from College Faculty and Staff data
	public static void createNewRow(Map<String,List<Contact>> map, String string, TableLayout layout, Context className){
	  	for (Map.Entry<String,List<Contact>> entry : map.entrySet())
    	{
    	    List<Contact> links = entry.getValue();
    	    DynamicLayoutCreation.createNewRow(entry.getKey(), layout, className);
    	    for(Contact contact : links)
    	    {
    	    	 DynamicLayoutCreation.createNewRow(contact.name, layout, className);
    	    	 DynamicLayoutCreation.createNewRow(contact.phoneNumber, layout, className);
    	    	 DynamicLayoutCreation.createNewRow(contact.email, layout, className);
    	    	 DynamicLayoutCreation.createNewRow("", layout, className);
    	    }
    	}
	}
	
	private static OnClickListener btnLinkListener = new View.OnClickListener(){
    	@Override
    	public void onClick(View v){
    		Intent intent = null;
    		Uri uri = null;
    		Button btn = (Button)v;
    		String tag = (String) btn.getTag();
    		String googleDocsUrl = "http://docs.google.com/viewer?url=";
    		Context contextName = btn.getContext();
    		
    		if(tag.substring(0,4).contentEquals("page"))	//open specified Activity
    		{
    			String[] tagParts = tag.split("\\::");
    			@SuppressWarnings("rawtypes")
				Class goToClass = MainActivity.class;
    			if (tagParts[0].contentEquals("page2")){
    				goToClass = CurrentStudentsPage2.class;
    			}
    			else if (tagParts[0].contentEquals("page2colleges")){
    				goToClass = CollegesPage2.class;
    			}
    			else if (tagParts[0].contentEquals("page3")){
    				goToClass = CurrentStudentsPage3.class;
    			}
    			else if (tagParts[0].contentEquals("page1")){
    				goToClass = CurrentStudents.class;
    			}
    			else{
    				goToClass= MainActivity.class;
    			}
    			intent = new Intent(btn.getContext(), goToClass);
    			intent.putExtra("URL",tagParts[1]);
    		}
    		else if (tag.contains("http"))
    		{
    			if (tag.endsWith(".pdf"))
    			{
    				tag = googleDocsUrl + tag;		//opens with google docs (doesn't work with just link)
    			}
    			uri = Uri.parse(tag);
    			intent = new Intent(Intent.ACTION_VIEW, uri);
    		}
    		else
    		{
    		}

    		contextName.startActivity(intent);
    	}
    };
  
}
